package com.hengtiansoft.lfy.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hengtiansoft.lfy.R;
import com.hengtiansoft.lfy.constants.Constants;
import com.hengtiansoft.lfy.entity.NewsEntity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by daiye on 2016/10/24 0024.
 */

public class NewsAdapter extends BaseAdapter {
    private Activity mContext = null;
    private List<NewsEntity.DataEntity> newsEntities = new ArrayList<>();
    private DisplayImageOptions defaultOptions;

    public NewsAdapter(Activity context) {
        this.mContext = context;
        defaultOptions = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .build();
    }

    @Override
    public int getCount() {
        return newsEntities.size();
    }

    @Override
    public Object getItem(int position) {
        return newsEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setData(List<NewsEntity.DataEntity> newsEntities) {
        this.newsEntities = newsEntities;
        notifyDataSetChanged();
    }

    public void addData(List<NewsEntity.DataEntity> newsEntities) {
        this.newsEntities.addAll(newsEntities);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.newsEntities.clear();
    }

    public List<NewsEntity.DataEntity> getData() {
        return this.newsEntities;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        NewsEntity.DataEntity newEntity = (NewsEntity.DataEntity) getItem(position);
        ViewHolder viewholder = null;
        if (convertView == null) {
            viewholder = new ViewHolder();
            convertView = mContext.getLayoutInflater().inflate(R.layout.layout_news_item_one_pic, parent, false);
            viewholder.tv_new_title = (TextView) convertView.findViewById(R.id.tv_new_title);
            viewholder.layout_photo = (ImageView) convertView.findViewById(R.id.layout_photos);
            viewholder.tv_creator = (TextView) convertView.findViewById(R.id.tv_creator);
            convertView.setTag(viewholder);
        } else {
            viewholder = (ViewHolder) convertView.getTag();
        }

        if (newEntity != null) {
            String pic = newEntity.getPic();
            String create_time = newEntity.getCreate_time();
            String creator = newEntity.getCreator();
            String source = newEntity.getSource();
            String title = newEntity.getTitle();
            String sday = newEntity.getS_day();
            if (!TextUtils.isEmpty(pic)) {
                viewholder.layout_photo.setVisibility(View.VISIBLE);
                String url = Constants.SERVER_HOST + "images/news/title/" + pic;
                ImageLoader.getInstance().displayImage(url, viewholder.layout_photo, defaultOptions);
            } else
                viewholder.layout_photo.setVisibility(View.GONE);
            if (!TextUtils.isEmpty(title)) {
                viewholder.tv_new_title.setText(title);
            } else
                viewholder.tv_new_title.setText("");
            String creatortext = "";
            if (!TextUtils.isEmpty(creator)) {
                creatortext += creator;
            }
            if (!TextUtils.isEmpty(sday)) {
                creatortext += "    " + sday;
            }
            if (!TextUtils.isEmpty(source)) {
                creatortext += "    " + source;
            }
            viewholder.tv_creator.setText(creatortext);
        }

        return convertView;
    }

    private class ViewHolder {
        private TextView tv_new_title;
        private ImageView layout_photo;
        private TextView tv_creator;
    }
}
